package com.steven.ourcontest.widget

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.steven.ourcontest.R
import com.steven.ourcontest.net.NetConstants
import kotlinx.android.synthetic.main.dialog_enlarge_image.*
import java.lang.IllegalStateException

class EnlargeImageDialog (
    context: Context,
    themeId: Int = R.style.dialog
) :
    Dialog(context, themeId) {

    companion object {
        private var urlStr: String? = null
    }

    init {
        setContentView(R.layout.dialog_enlarge_image)
        setCancelable(true)
        window?.setGravity(Gravity.CENTER)
        window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    fun setUrl(url: String): EnlargeImageDialog {
        urlStr = url
        return this
    }

    override fun show() {
        super.show()
        if (urlStr == null) {
            throw IllegalStateException("url不能为空")
        }
        Glide.with(context)
            .load(NetConstants.ICON_BASE_URL + urlStr)
            .placeholder(R.drawable.ic_placeholder)
            .dontAnimate()
            .error(R.drawable.aio_image_fail)
            .into(iv_image)
    }
}